local M = {}
local t = require('utils.table')

-- ret: buffer handle
M.get_current = function()
    return vim.api.nvim_get_current_buf()
end

M.get_current_filename = function()
    return M.get_filename(M.get_current())
end

M.get_filename = function(bufnr)
    return vim.api.nvim_buf_get_name(bufnr)
end

-- param: buffer handle
M.is_modified = function(bufnr)
    -- return vim.api.nvim_buf_get_option(bufnr, 'modified')
    return vim.api.nvim_get_option_value('modified', {buf=bufnr})
end

-- ret array of buffer handles that show up in the buffer list
-- note: vim.api.nvim_list_bufs: Gets the current list of buffer handles
M.get_listed_bufs = function()
    return vim.tbl_filter(function(bufnr)
        return vim.api.nvim_buf_get_option(bufnr, "buflisted")
    end, vim.api.nvim_list_bufs())
end

M.get_file_type = function(bufnr)
    if bufnr == 0 then
        return vim.api.nvim_buf_get_option(M.get_current(),'filetype')
    end
    return vim.api.nvim_buf_get_option(bufnr,'filetype')
end

M.get_current_file_type = function()
    return M.get_file_type(0)
end

M.close_all = function()
    local buffers = M.get_listed_bufs()
    t.foreach(buffers,function(buf)
        if M.is_modified(buf) then
            return
        end
        vim.cmd('silent! bdelete ' .. buf)
    end)
    if M.is_modified(M.get_current()) then
        return
    end
    vim.cmd('Alpha') -- avoid show here.term window
end

return M
-- EOP
